package cn.benma666.sjsj.ljq.fzgj;

import cn.benma666.exception.MyException;
import cn.benma666.iframe.BasicObject;
import cn.benma666.iframe.MyParams;
import cn.benma666.iframe.Result;
import cn.benma666.myutils.FileUtil;
import cn.benma666.sjsj.web.LjqInterface;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.FileWriter;

/**
 * * ssl证书相关接口
 */
@Component("SYS_COMMON_SSL")
@Scope("prototype")
public class SslLjq extends BasicObject implements LjqInterface {
    /**
     * Domain Admin平台通过接口自动部署证书
     * @param myParams 参数
     * @return 结果
     */
    public Result daZdbs(MyParams myParams){
        final String sslml = myParams.getString("$.da.sslml");
        final String domain = myParams.getJSONArray("domains").getString(0);
        FileWriter fw = null;
        try {
            fw = new FileWriter(FileUtil.getFilePath(sslml, domain+".key"));
            fw.write(myParams.getString("ssl_certificate_key"));
            fw.close();
            fw = new FileWriter(FileUtil.getFilePath(sslml, domain+".pem"));
            fw.write(myParams.getString("ssl_certificate"));
            fw.close();
        }catch (Exception e){
            throw new MyException("自动部署证书失败",e);
        }finally {
            FileUtil.closeStream(fw);
        }
        return success("自动部署成功");
    }
}
